package com.funny.learning.english.activity;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.model.GameEntity;
import com.funny.learning.english.model.HistoryEntity;

public class LangNgheActivity extends GameBaseActivity implements OnClickListener {
	
	private String TAG = "LangNgheActivity";

	private RelativeLayout rl1, rl2, rl3, rl4;
	private TextView tv1, tv2, tv3, tv4, tvFull1, tvFull2, tvFull3, tvFull4;
	private ImageView iv1, iv2, iv3, iv4;

	private int countTrue = 0;


	private int posDung = -1;

	private String srcSound;
	private ImageView ivSound;
	private Animation animation1;
	private Animation animation2;
	private Animation animation3;
	private Animation animation4;
	
	private GameEntity gameEntity1;
	private GameEntity gameEntity2;
	private GameEntity gameEntity3;
	private GameEntity gameEntity4;
	private GameEntity gameEntityDapAn;
	
	private ArrayList<GameEntity> arrGameEntities;
	
	private boolean isPause = false;

	private ImageView ivPause;

	private ImageView ivPlay;

	private ImageView ivRefresh;

	private ImageView ivBack;

	private LinearLayout llPause;

	private TextView tvTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.lang_nghe_chon_layout);
		super.onCreate(savedInstanceState);
	}

	private void checkDapAn(final Animation animation,
			final RelativeLayout relativeLayout, final TextView textView,
			final TextView textViewFull, final ImageView imageView) {
		relativeLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

//				int getTag = (Integer) relativeLayout.getTag();
				String text = textView.getText().toString();
				if (text.equals(gameEntityDapAn.getLname())) {
					textView.setVisibility(View.GONE);
					textViewFull.setVisibility(View.VISIBLE);
					// imageView.setImageResource(R.drawable.image1);
					relativeLayout.startAnimation(animation);

					animation.setAnimationListener(new AnimationListener() {

						@Override
						public void onAnimationStart(Animation arg0) {

						}

						@Override
						public void onAnimationRepeat(Animation arg0) {

						}

						@Override
						public void onAnimationEnd(Animation arg0) {
							if(countCheckScreen < countTrue){
								countCheckScreen = countCheckScreen + 1;
								countAnwserCorrect = countAnwserCorrect + 1;
							}
							countTrue = countTrue + 1;
							if (countTrue >= mGameEntities.size()) {
								finishGame();
								HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGameName, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopicName, slugSubTopicName);
								mHistoryDAO.saveHistory(historyEntity);
							} else {
								arrGameEntities.clear();
								initData(countTrue);
							}
						}
					});
				} else {
					if(countCheckScreen < countTrue){
						countCheckScreen = countCheckScreen + 1;
					}
					textView.setVisibility(View.VISIBLE);
					textViewFull.setVisibility(View.GONE);
					relativeLayout
							.setBackgroundResource(R.drawable.chon_tu_sai);
				}
			}
		});
	}
	
	private void refreshView(){
		 rl1.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		 rl2.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		 rl3.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		 rl4.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		
		 tvFull1.setVisibility(View.GONE);
		 tvFull2.setVisibility(View.GONE);
		 tvFull3.setVisibility(View.GONE);
		 tvFull4.setVisibility(View.GONE);
		
		 tv1.setVisibility(View.VISIBLE);
		 tv2.setVisibility(View.VISIBLE);
		 tv3.setVisibility(View.VISIBLE);
		 tv4.setVisibility(View.VISIBLE);
		 
		 gameEntity1 = null;
		 gameEntity2 = null;
		 gameEntity3 = null;
		 gameEntity4 = null;
		 gameEntityDapAn = null;
	}

	// private void refeshData(int count){
	//
	// rl1.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
	// rl2.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
	// rl3.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
	// rl4.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
	//
	// tvFull1.setVisibility(View.GONE);
	// tvFull2.setVisibility(View.GONE);
	// tvFull3.setVisibility(View.GONE);
	// tvFull4.setVisibility(View.GONE);
	//
	// tv1.setVisibility(View.VISIBLE);
	// tv2.setVisibility(View.VISIBLE);
	// tv3.setVisibility(View.VISIBLE);
	// tv4.setVisibility(View.VISIBLE);
	//
	//
	// String str1 = arrData.get(count).getStr1();
	// String str2 = arrData.get(count).getStr2();
	// String str3 = arrData.get(count).getStr3();
	// String str4 = arrData.get(count).getStr4();
	//
	// int srcImg1 = arrData.get(count).getSrcImg1();
	// int srcImg2 = arrData.get(count).getSrcImg2();
	// int srcImg3 = arrData.get(count).getSrcImg3();
	// int srcImg4 = arrData.get(count).getSrcImg4();
	//
	// srcSound = arrData.get(count).getSrcRaw();
	//
	// posDung = arrData.get(count).getPosDung();
	//
	// tv1.setText(str1);
	// tv2.setText(str2);
	// tv3.setText(str3);
	// tv4.setText(str4);
	//
	// tvFull1.setText(str1);
	// tvFull2.setText(str2);
	// tvFull3.setText(str3);
	// tvFull4.setText(str4);
	//
	// iv1.setImageResource(srcImg1);
	// iv2.setImageResource(srcImg2);
	// iv3.setImageResource(srcImg3);
	// iv4.setImageResource(srcImg4);
	//
	// }

	@Override
	protected void initSucess() {
		super.initSucess();
		
		ivPause = (ImageView)findViewById(R.id.ivPause);
		ivPlay = (ImageView)findViewById(R.id.ivPlay);
		ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
		ivBack = (ImageView)findViewById(R.id.ivBack);
		llPause = (LinearLayout)findViewById(R.id.llPause);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		
//		ivBack.setOnClickListener(this);
//		ivPause.setOnClickListener(this);
//		ivPlay.setOnClickListener(this);
//		ivRefresh.setOnClickListener(this);
		
		setTitle("Lắng nghe và chọn");
		
		initUI();
		if(mGameEntities.size() <= 0){
			onBackPressed();
			return;
		}
		GameEntity gameEntity = mGameEntities.get(0);
		
		String img = getPathImg(gameEntity);

		String sound = getPathSound(gameEntity);
		
		initData(countTrue);
		
		checkDapAn(animation1, rl1, tv1, tvFull1, iv1);
		checkDapAn(animation1, rl2, tv2, tvFull2, iv2);
		checkDapAn(animation1, rl3, tv3, tvFull3, iv3);
		checkDapAn(animation1, rl4, tv4, tvFull4, iv4);
		
		

	}
	
	private void initData(int count){
		refreshView();
		arrGameEntities = new ArrayList<GameEntity>();

		GameEntity gameEntity = mGameEntities.get(count);
		
		srcSound = getPathSound(gameEntity);

		gameEntityDapAn = gameEntity;
		int countRandom = getNumberRandom();
		if (countRandom == 1) {
			gameEntity1 = gameEntityDapAn;
			Log.d(TAG, "gameEntity1.getLname():" + gameEntity1.getLname());
			Log.d(TAG, "count:" + countTrue);
			if(gameEntity1.getLname() == null){
				countTrue = countTrue + 1;
				if(countTrue >= mGameEntities.size()){
					finishGame();
				}else{
					initData(countTrue);
				}
			}else{
				arrGameEntities.add(gameEntity1);
				while (gameEntity2 == null) {
					gameEntity2 = getRandomGameEntity();
				}
				while (gameEntity3 == null) {
					gameEntity3 = getRandomGameEntity();
				}
				while (gameEntity4 == null) {
					gameEntity4 = getRandomGameEntity();
				}
			}
		} else if (countRandom == 2) {
			gameEntity2 = gameEntityDapAn;
			Log.d(TAG, "gameEntity2.getLname():" + gameEntity2.getLname());
			Log.d(TAG, "count:" + countTrue);
			if(gameEntity2.getLname() == null){
				countTrue = countTrue + 1;
				if(countTrue >= mGameEntities.size()){
					finishGame();
				}else{
					initData(countTrue);
				}
				
			}else{
				arrGameEntities.add(gameEntity2);
				while (gameEntity1 == null) {
					gameEntity1 = getRandomGameEntity();
				}
				while (gameEntity3 == null) {
					gameEntity3 = getRandomGameEntity();
				}
				while (gameEntity4 == null) {
					gameEntity4 = getRandomGameEntity();
				}
			}
			
			

		} else if (countRandom == 3) {
			gameEntity3 = gameEntityDapAn;
			Log.d(TAG, "gameEntity3.getLname():" + gameEntity3.getLname());
			Log.d(TAG, "count:" + countTrue);
			if(gameEntity3.getLname() == null){
				countTrue = countTrue + 1;
				if(countTrue >= mGameEntities.size()){
					finishGame();
				}else{
					initData(countTrue);
				}
				
			}else{
				arrGameEntities.add(gameEntity3);

				while (gameEntity1 == null) {
					gameEntity1 = getRandomGameEntity();
				}
				while (gameEntity2 == null) {
					gameEntity2 = getRandomGameEntity();
				}
				while (gameEntity4 == null) {
					gameEntity4 = getRandomGameEntity();
				}
			}
			
		} else {
			gameEntity4 = gameEntityDapAn;
			Log.d(TAG, "gameEntity4.getLname():" + gameEntity4.getLname());
			Log.d(TAG, "count:" + countTrue);
			if(gameEntity4.getLname() == null){
				countTrue = countTrue + 1;
				if(countTrue >= mGameEntities.size()){
					finishGame();
				}else{
					initData(countTrue);
				}
			}else{
				arrGameEntities.add(gameEntity4);

				while (gameEntity1 == null) {
					gameEntity1 = getRandomGameEntity();
				}
				while (gameEntity3 == null) {
					gameEntity3 = getRandomGameEntity();
				}
				while (gameEntity2 == null) {
					gameEntity2 = getRandomGameEntity();
				}
			}
			
		}
		
		 tv1.setText(gameEntity1.getLname());
		 tv2.setText(gameEntity2.getLname());
		 tv3.setText(gameEntity3.getLname());
		 tv4.setText(gameEntity4.getLname());
		
		 tvFull1.setText(gameEntity1.getLname());
		 tvFull2.setText(gameEntity2.getLname());
		 tvFull3.setText(gameEntity3.getLname());
		 tvFull4.setText(gameEntity4.getLname());
		 
		 
		 Bitmap bm1 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity1)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		 Bitmap bm2 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity2)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		 Bitmap bm3 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity3)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		 Bitmap bm4 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity4)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		 
		 iv1.setImageBitmap(bm1);
		 iv2.setImageBitmap(bm2);
		 iv3.setImageBitmap(bm3);
		 iv4.setImageBitmap(bm4);
		
//		 iv1.setImageBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity1)));
//		 iv2.setImageBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity2)));
//		 iv3.setImageBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity3)));
//		 iv4.setImageBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity4)));
		 
		
	}
	
	private void initUI(){
		intAB("Lắng nghe và chọn");

		animation1 = AnimationUtils.loadAnimation(
				LangNgheActivity.this, R.anim.g_mw_correct);
		animation2 = AnimationUtils.loadAnimation(
				LangNgheActivity.this, R.anim.g_mw_correct);
		animation3 = AnimationUtils.loadAnimation(
				LangNgheActivity.this, R.anim.g_mw_correct);
		animation4 = AnimationUtils.loadAnimation(
				LangNgheActivity.this, R.anim.g_mw_correct);
		// addData();

		rl1 = (RelativeLayout) findViewById(R.id.rl1);
		rl2 = (RelativeLayout) findViewById(R.id.rl2);
		rl3 = (RelativeLayout) findViewById(R.id.rl3);
		rl4 = (RelativeLayout) findViewById(R.id.rl4);

		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv31);
		tv4 = (TextView) findViewById(R.id.tv41);
		tvFull1 = (TextView) findViewById(R.id.tvFull1);
		tvFull2 = (TextView) findViewById(R.id.tvFull2);
		tvFull3 = (TextView) findViewById(R.id.tvFull3);
		tvFull4 = (TextView) findViewById(R.id.tvFull4);

		iv1 = (ImageView) findViewById(R.id.iv1);
		iv2 = (ImageView) findViewById(R.id.iv2);
		iv3 = (ImageView) findViewById(R.id.iv3);
		iv4 = (ImageView) findViewById(R.id.iv4);
		ivSound = (ImageView) findViewById(R.id.ivSound);
		
		ivSound.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Toast.makeText(activity, "ivSound:: click InitDatta", 1000).show();
				Log.d(TAG, "srcSound::" + mGameEntities.get(countTrue).getLaudio());
				playMusic(getPathSound(gameEntityDapAn));
			}
		});
		
		
		
	}
	
	 private int getNumberRandom(){
			Random ran = new Random();
			int x = ran.nextInt(4);
			
			Log.d(TAG, "number:" + x);
			
			return x;
		}
		
		private GameEntity getRandomGameEntity(){
			GameEntity gameEntity = mGameEntities.get(new Random().nextInt(mGameEntities.size()));
			
//			Log.d(TAG, "getRandomGameEntity gameEntity::" + gameEntity.getLname());
			if(gameEntity.getLname() == null || gameEntity.getLname().equals("")){
				return null;
			}else{
				if(!arrGameEntities.contains(gameEntity)){
					arrGameEntities.add(gameEntity);
					return gameEntity;
				}else{
//					getRandomGameEntity();
					return null;
				}
			}
			
		}

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.ivPause:
				isPause = !isPause;
				if(isPause){
					ivPause.setImageResource(R.drawable.ic_play);
					llPause.setVisibility(View.VISIBLE);
				}else{
					ivPause.setImageResource(R.drawable.ic_pause);
					llPause.setVisibility(View.GONE);
				}
				break;
			case R.id.ivPlay:
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
				break;
			case R.id.ivRefresh:
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
				countTrue = 0;
				initData(countTrue);
				break;
			case R.id.ivBack:
				onBackPressed();
				break;

			default:
				break;
			}
		}
}
